enum ActionType {
    rotate = 0, //旋转
    shake = 1 ,// 抖动
    scale = 2 ,// 呼吸动作
    alpha = 3 ,// 
}

// enum EaseType{
//     Line = "",
//     Sin = "sineInOut",

// }
const { ccclass, property, menu, disallowMultiple, executionOrder, requireComponent } = cc._decorator;
// 这个类用来写动作

@ccclass
//@disallowMultiple() //防止多个相同类型（或子类型）的组件被添加到同一个节点
@menu('工具组件/TweenAnim')
export default class TweenAnim extends cc.Component {

    @property({type:cc.Enum(ActionType),displayName:"动作类型"})
    actionType:ActionType = ActionType.rotate;

    @property({displayName:"是否立即开始"})
    isAutoStart:boolean = true;

    @property({type:cc.Float,displayName:"前往最大值的时间",visible(){return this.actionType !== ActionType.shake}})
    private maxTime = .5;

    @property({type:cc.Float,displayName:"前往最小值的时间",visible(){return this.actionType !== ActionType.shake}})
    private minTime = .5;

    @property({type:cc.Float,displayName:"最大值",visible(){return this.actionType == ActionType.scale}})
    maxScale:number = 1.1;

    @property({type:cc.Float,displayName:"最小值",visible(){return this.actionType == ActionType.scale}})
    minScale:number = 0.9;

    @property({type:cc.Integer,displayName:"透明度最大值",visible(){return this.actionType == ActionType.alpha}})
    maxAlpha:number = 255;

    @property({type:cc.Integer,displayName:"透明度最小值",visible(){return this.actionType == ActionType.alpha}})
    minAlpha:number = 0;

    @property({displayName:"反向",visible(){return this.actionType == ActionType.rotate}})
    private inverse : boolean = false;

    // LIFE-CYCLE CALLBACKS:

    private startPosX = 0
    private startPosY = 0
    private startAngle = 0 // 初始角度
    private isRun = false
    onLoad () {
        this.startPosX = this.node.x
        this.startPosY = this.node.y
        this.startAngle = this.node.angle

        if (this.isAutoStart){
            this.runAction()
        }
    }

    start () {
        if (this.isAutoStart){
            this.runAction()
        }
    }

    onStart(){
        this.runAction()
    }

    runAction(){
        if(this.isRun){return;}
        this.isRun = true;
        this.node.stopAllActions()
        let tween = cc.tween(this.node);
        if (this.actionType == ActionType.rotate){
            this.node.runAction(cc.repeatForever(cc.rotateBy(0.1,this.inverse ? 10:-10)))
        }else if(this.actionType == ActionType.shake){
            var time = 0.1
            var offset = 10
            var action1 = cc.rotateBy(time,offset)
            var action2 = cc.rotateBy(time,-offset*2)
            var action3 = cc.rotateBy(time,offset*2)
            var action4 = cc.rotateBy(time,-offset*2)
            var action5 = cc.rotateBy(time,offset*2)
            var action6 = cc.rotateBy(time,-offset)
            var action7 = cc.delayTime(0.5)
            var repeat1 = cc.repeatForever(cc.sequence(action1, action2,action3,action4,action5,action6,action7))
            this.node.runAction(repeat1)
        }else if (this.actionType == ActionType.scale){
            tween.repeatForever(tween.sequence(tween.to(this.maxTime,{scale:this.maxScale},{easing:'sineInOut'}),tween.to(this.minTime,{scale:this.minScale},{easing:'sineInOut'}))).start();
        }else if(this.actionType == ActionType.alpha){
            tween.repeatForever(tween.sequence(tween.to(this.maxTime,{opacity:this.maxAlpha},{easing:'sineInOut'}),tween.to(this.minTime,{opacity:this.minAlpha},{easing:'sineInOut'}))).start();
        }
    }

    onStop(){
        this.isRun = false
        this.node.stopAllActions()
        this.node.x = this.startPosX
        this.node.y = this.startPosY
        this.node.angle = this.startAngle
        this.node.scale = 1;
        this.node.opacity = 255;
    }

    // update (dt) {}
}
